package com.hawthorn.admin.repository.provider;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.builder.annotation.ProviderMethodResolver;

import java.util.Map;

/**
 * <p>todo sysrole provider sql</p>
 * @author: andy.ten@tom.com | xukaixing@hotmail.com
 * @created: 2022/7/29 10:26
 * @version v1.0.1
 * @copyright: Copyright (c)  andyten
 */
@Slf4j
public class SysRoleSqlProvider implements ProviderMethodResolver
{
  public static String findRoles(Page<Map<String, Object>> page, String conditions)
  {
    StringBuffer sql = new StringBuffer();
    sql.append(" select\n ");
    sql.append(" 	id,\n ");
    sql.append(" 	rcode,\n ");
    sql.append(" 	rname,\n ");
    sql.append(" 	ename,\n ");
    sql.append(" 	level,\n ");
    sql.append(" 	type,\n ");
    sql.append(" 	status,\n ");
    sql.append(" 	remark,\n ");
    sql.append(" 	dept_id deptId,\n ");
    sql.append(" 	create_time createTime,\n ");
    sql.append(" 	company_id companyId,\n ");
    sql.append(" 	oem_company_id oemCompanyId \n ");
    sql.append(" from\n ");
    sql.append(" 	sys_role \n ");
    sql.append(" where ");
    sql.append(conditions);
    return sql.toString();
  }

  public static String findDealerRoles(Page<Map<String, Object>> page, String conditions)
  {
    StringBuffer sql = new StringBuffer();
    sql.append(" select\n ");
    sql.append(" 	id,\n ");
    sql.append(" 	rcode,\n ");
    sql.append(" 	rname,\n ");
    sql.append(" 	ename,\n ");
    sql.append(" 	level,\n ");
    sql.append(" 	type,\n ");
    sql.append(" 	status,\n ");
    sql.append(" 	remark,\n ");
    sql.append(" 	dept_id deptId,\n ");
    sql.append(" 	create_time createTime,\n ");
    sql.append(" 	company_id companyId,\n ");
    sql.append(" 	oem_company_id oemCompanyId \n ");
    sql.append(" from\n ");
    sql.append(" 	sys_role \n ");
    sql.append(" where ");
    sql.append(conditions);
    // sql.append(" union  \n ");
    // sql.append(" select \n ");
    // sql.append("   	id, \n ");
    // sql.append("   	rcode, \n ");
    // sql.append("   	rname, \n ");
    // sql.append("   	ename, \n ");
    // sql.append("   	level, \n ");
    // sql.append("   	type, \n ");
    // sql.append("   	status, \n ");
    // sql.append("   	remark, \n ");
    // sql.append("   	dept_id deptid , \n ");
    // sql.append("   	create_time createtime , \n ");
    // sql.append("   	company_id companyid , \n ");
    // sql.append("   	oem_company_id oemcompanyid  \n ");
    // sql.append("   from \n ");
    // sql.append("   	sys_role  \n ");
    // sql.append("   where  rcode in ('dealersalesman','dealerwaremng','dealerinternal') \n ");
    return sql.toString();
  }

  public String findRoleByCode(String rcode)
  {
    StringBuffer sql = new StringBuffer();
    sql.append(" select\n ");
    sql.append(" count( id ) \n ");
    sql.append(" from\n ");
    sql.append(" sys_role \n ");
    sql.append(" where\n ");
    sql.append(" rcode = " + "'" + rcode + "' \n");
    return sql.toString();
  }

  public String findDealerRoleByCode(String rcode, Long deptId)
  {
    StringBuffer sql = new StringBuffer();
    sql.append(" select\n ");
    sql.append(" count( id ) \n ");
    sql.append(" from\n ");
    sql.append(" sys_role \n ");
    sql.append(" where\n ");
    sql.append(" dept_id = " + deptId + "\n");
    sql.append(" and rcode = " + "'" + rcode + "' \n");
    return sql.toString();
  }

  /**
   * 查询已授权的菜单
   * @param roleId
   * @return
   */
  public String searchAuthGrantMenus(Long roleId)
  {
    StringBuffer sql = new StringBuffer();
    sql.append(" select\n ");
    sql.append(" r.role_id roleId, \n ");
    sql.append(" r.menu_id menuId, \n ");
    sql.append(" r.perms, \n ");
    sql.append(" m.type \n ");
    sql.append(" from \n ");
    sql.append(" sys_role_menu r, sys_menu m \n ");
    sql.append(" where \n ");
    sql.append(" m.id = r.menu_id \n ");
    sql.append(" and r.role_id = " + roleId);
    sql.append(" order by role_id, menu_id ");

    return sql.toString();
  }

  /**
   * 根据roleId查询用户
   * @param roleId
   * @return
   */
  public String findUserByRoleId(Long roleId)
  {
    StringBuffer sql = new StringBuffer();
    sql.append(" select\n ");
    sql.append(" r.user_id userId, \n ");
    sql.append(" r.user_account userAccount \n ");
    sql.append(" from sys_user_role r \n ");
    sql.append(" where role_id = " + roleId + " \n ");
    return sql.toString();
  }

}
